<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { GetData, PatchModifyType } from '@/api/modules/birthday'
import PageNumber from '@/store/modules/PageNumberCache'

const imgDomain = 'https://img1.eprhan.cc'
const router = useRouter()
const PageNumberCache = PageNumber()

const findFrom = ref({
  ProductId: '',
})
const dataList: any = reactive({
  list: [],
})
const loading = ref(true)
const total = ref(0)
let PageSize = 20

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.ProductId && ({ ProductId: findFrom.value.ProductId })),
  }
  await GetData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      dataList.list.bannerPics = dataList.list.map((item: any) => {
        item.bannerPics = item.bannerPics.split(',')
        return item
      })
      total.value = dataRes.data.total
      loading.value = false
    }
  })
  loading.value = false
}

// 修改状态
async function ModifyType(params: any) {
  // const params = {
  //   actDrawId: 1000100,
  //   actStatus: 1,
  // }
  params.flag === 1 ? params.flag = true : params.flag = false

  await PatchModifyType(params).then((res: any) => {
    if (res.statusCode === 200) {
      ElMessage.success('修改成功')
      GetDataList(PageNumberCache.GetBirthdayPage(), PageSize)
    }
    else {
      ElMessage.error(`修改失败${res.errors}`)
    }
  })
}

// 查询
function Inquire() {
  GetDataList(1, PageSize)
}

// 添加
function AddData() {
  router.push({
    name: 'birthdayDetail',
  })
}

// 编辑
function EditData(id: number) {
  router.push({
    name: 'birthdayDetail',
    query: {
      id,
    },
  })
}

// // 修改排序
// function SortModification(message: any) {
//   const params = {
//     id: message.row.id,
//     sort: message.row.sort,
//   }
//   patchListSort(params).then((res) => {
//     if (res.data) {
//       ElMessage.success('修改成功。')
//     }
//     else {
//       ElMessage.error('修改失败。')
//       GetDataList()
//     }
//   })
// }

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  PageSize = val
}
function handleCurrentChange(val: number) {
  GetDataList(val, PageSize)
  PageNumberCache.SetMerchandiseControlPage(val)
}

const nowPageNum = computed(() => {
  return PageNumberCache.GetBirthdayPage()
})

onMounted(() => {
  GetDataList(PageNumberCache.GetBirthdayPage(), 20)
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="商品ID">
                <el-input v-model="findFrom.ProductId" style="width: 300px;" placeholder="请输入商品ID" @keyup.enter="Inquire" />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <el-button type="success" @click="AddData">
          添加
        </el-button>
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="ID" width="100" />
        <el-table-column header-align="center" prop="shareTxt" label="分享语" width="350" />
        <el-table-column align="center" label="分享图" width="200">
          <template #default="scope">
            <ElImage style=" width: auto;height: 60px;" :src="imgDomain + scope.row.sharePic" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="企微弹层" width="200">
          <template #default="scope">
            <ElImage style=" width: auto;height: 60px;" :src="imgDomain + scope.row.qvPic" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="productIds" label="关联商品ID" width="120" />
        <el-table-column align="center" prop="bannerPics" label="领券背景图" width="300">
          <template #default="scope">
            <el-scrollbar height="60px">
              <div class="scrollbar-flex-content">
                <el-image v-for="(item, index) in scope.row.bannerPics" :key="index" class="scrollbar-flex-img" :src="imgDomain + item" />
              </div>
            </el-scrollbar>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="addTime" label="添加时间" width="200" />
        <el-table-column align="center" prop="updateTime" label="更新时间" width="200" />
        <el-table-column align="center" label="是否上架" fixed="right" width="120">
          <template #default="scope">
            <el-switch
              v-model="scope.row.flag"
              :active-value="1"
              :inactive-value="0"
              @click="ModifyType({ id: scope.row.id, flag: scope.row.flag })"
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right" min-width="150">
          <template #default="scope">
            <el-button type="primary" @click="EditData(scope.row.id)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="20"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.scrollbar-flex-content {
  display: flex;
  max-width: 300px;

  .scrollbar-flex-img {
    display: flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    width: 100px;
    height: auto;
    margin-right: 10px;
  }
}
</style>
